package org.happy.tester.catcher.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.happy.tester.catcher.service.CollectService;
import org.happy.tester.catcher.util.JenkinsUtil;
import org.happy.tester.catcher.util.LocalCacheUtil;
import org.happy.tester.common.http.req.CollectResultRequest;
import org.happy.tester.common.http.req.PointcutRequest;
import org.happy.tester.common.model.PointcutDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.happy.tester.common.constant.CommonConstant.POINTCUT_JOINER;

/**
 * @Classname PointCut
 * @Description TODO:将 pointcut 标记存入数据库
 * @Date 2020/4/5 08:54 下午
 * @Created by jc.bai
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Resource
    private JenkinsUtil jenkinsUtil;

    @Override
    public boolean needCollect(String pointcut) {
        List<String> needCollectPointcuts = LocalCacheUtil.POINTCUTS_CACHE.get(LocalCacheUtil.NEED_COLLECT);
        boolean contains = Optional.ofNullable(needCollectPointcuts).orElse(new ArrayList<>()).contains(pointcut);
        return contains;
    }

    /**
     * 1、标记需要 collect 的 pointcut
     * 2、将 pointcut 从 needMock 中移除
     *
     * @param pointcutList
     */
    @Override
    public void markCollect(List<PointcutRequest> pointcutList) {
        LocalCacheUtil.POINTCUTS_CACHE.put(LocalCacheUtil.NEED_COLLECT, new ArrayList<>());
        pointcutList.stream().forEach(pointcutRequest -> {
            List<PointcutDO> pointcutDOS = pointcutRequest.getPointcutDOS();
            pointcutDOS.stream().forEach(pointcutDO -> {
                String paramTypes = null;
                if (pointcutDO.getParamsTypeArray() != null) {
                    paramTypes = Arrays.toString(pointcutDO.getParamsTypeArray());
                    paramTypes =  paramTypes.substring(1, paramTypes.length() - 1);
                }
                String pointcut = POINTCUT_JOINER.join(pointcutRequest.getAppIp(),
                        pointcutDO.getClassName(),
                        pointcutDO.getMethodName(),
                        paramTypes,
                        pointcutRequest.getClientIp());
                //mark need collect
                LocalCacheUtil.POINTCUTS_CACHE.get(LocalCacheUtil.NEED_COLLECT).add(pointcut);
                try {
                    jenkinsUtil.deployAgentLoader(pointcutRequest);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        });
    }

    @Override
    public void saveCollectResult(CollectResultRequest collectResultRequest) {
        LocalCacheUtil.RESULT_COLLECTED_CACHE.put(collectResultRequest.getPointcut(), collectResultRequest.getResult());
    }

    @Override
    public String fetchCollectResult(String pointcut) {
        String result = LocalCacheUtil.RESULT_COLLECTED_CACHE.get(pointcut);
        return result;
    }
}
